package com.kylin.core.commodity.event.repository;

import com.kylin.dal.entity.commodity.*;
import com.kylin.domain.commodity.event.CommodityCreatedEvent;
import com.kylin.domain.commodity.event.CommodityUpdatedEvent;
import com.kylin.domain.commodity.vo.AlbumVO;
import com.kylin.domain.commodity.vo.CommentVO;
import com.kylin.shared.enums.YesNoEnum;
import com.kylin.shared.utils.MappingTools;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作商品spu的数据查询功能
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年11月10日 14:55
 */
@Component
public final class CommodityDataHandler {

    private final JPAQueryFactory commodityFactory;
    private final QCommentEntry qCommentEntry;
    private final QCommodityAlbumEntry qAlbumEntry;
    private final QCommodityEntry qCommodityEntry;
    private final QCommodityHtmlEntry qCommodityHtmlEntry;

    private final CommodityRepository commodityRepository;
    private final CommentRepository commentRepository;
    private final CommodityAlbumRepository commodityAlbumRepository;
    private final CommodityHtmlRepository commodityHtmlRepository;

    public CommodityDataHandler(@Qualifier("kylinArtworkEntityManager")
                                        EntityManager entityManager,
                                CommentRepository commentRepository,
                                CommodityRepository commodityRepository,
                                CommodityAlbumRepository commodityAlbumRepository,
                                CommodityHtmlRepository commodityHtmlRepository) {
        this.commodityFactory = new JPAQueryFactory(entityManager);
        this.qCommodityEntry = QCommodityEntry.commodityEntry;
        this.qAlbumEntry = QCommodityAlbumEntry.commodityAlbumEntry;
        this.qCommentEntry = QCommentEntry.commentEntry;
        this.qCommodityHtmlEntry = QCommodityHtmlEntry.commodityHtmlEntry;

        this.commodityRepository = commodityRepository;
        this.commodityAlbumRepository = commodityAlbumRepository;
        this.commentRepository = commentRepository;
        this.commodityHtmlRepository = commodityHtmlRepository;
    }

    public void create(CommodityCreatedEvent event) {
        CommodityEntry entry = CommodityEntry.builder()
                .code(event.getCode())
                .title(event.getName())
                .subtitle(event.getSubtitle())
                .inventory(event.getInventory())
                .price(event.getPrice())
                .cover(event.getCoverUrl())
                .state(event.getApprovalState().getCode())
                .merchantId(event.getMerchantId())
                .removed(event.getRemoved().getCode())
                .shelved(event.getShelved().getCode())
                .createdAt(MappingTools.toEpochSecond(event.getCreatedAt()))
                .createdBy(event.getCreatedBy()).build();
        this.commodityRepository.saveAndFlush(entry);

        CommodityHtmlEntry htmlEntry = CommodityHtmlEntry.builder()
                .commodityId(entry.getCommodityId())
                .createdAt(MappingTools.toEpochSecond(event.getCreatedAt()))
                .createdBy(event.getCreatedBy())
                .description(event.getDescription())
                .build();
        this.commodityHtmlRepository.saveAndFlush(htmlEntry);
    }

    public void create(List<AlbumVO> galleries) {
        List<CommodityAlbumEntry> entries = new ArrayList<>();
        galleries.forEach(g -> {
            entries.add(CommodityAlbumEntry.builder()
                    .commodityId(g.getCommodityId())
                    .imgUrl(g.getImgUrl())
                    .removed(YesNoEnum.N.getCode())
                    .createdAt(MappingTools.toEpochSecond(g.getCreatedAt()))
                    .sort(g.getSort()).build());
        });
        this.commodityAlbumRepository.saveAll(entries);
    }

    public void create(CommentVO commentVO) {
        this.commentRepository.save(CommentEntry.builder()
                .commodityId(commentVO.getCommodityId())
                .content(commentVO.getContent())
                .removed(commentVO.getRemoved().getCode())
                .createdAt(MappingTools.toEpochSecond(commentVO.getCreatedAt()))
                .userId(commentVO.getUserId())
                // TODO: nickname & avatar
                .build());
    }

    public void update(CommodityUpdatedEvent event) {
        this.commodityFactory.update(qCommodityEntry)
                .set(qCommodityEntry.title, event.getName())
                .set(qCommodityEntry.subtitle, event.getSubtitle())
                .set(qCommodityEntry.cover, event.getCoverUrl())
                .set(qCommodityEntry.inventory, event.getInventory())
                .set(qCommodityEntry.price, event.getPrice())
                .set(qCommodityEntry.removed, event.getRemoved().getCode())
                .set(qCommodityEntry.shelved, event.getShelved().getCode())
                .set(qCommodityEntry.updatedBy, event.getUpdatedBy())
                .set(qCommodityEntry.updatedAt, MappingTools.toEpochSecond(event.getUpdatedAt()))
                .where(qCommodityEntry.commodityId.eq(event.getCommodityId()))
                .execute();
    }
}
